﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace LS.Dicom.Tools
{
    /// <summary>
    /// DicomViewer.xaml 的交互逻辑
    /// </summary>
    public partial class DicomViewer : UserControl
    {
        public static readonly DependencyProperty HasDicomFileProperty = DependencyProperty.Register("HasDicomFile", typeof(bool), typeof(DicomViewer));

        /// <summary>
        /// 是否已加载dicomFile
        /// </summary>
        public bool HasDicomFile { get => (bool)this.GetValue(HasDicomFileProperty); set => this.SetValue(HasDicomFileProperty, value); }
        public DicomViewer()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void OpenDicom_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == true)
            {
                HasDicomFile = false;
                dicomViewer.LoadDicomFile(dialog.FileName);
                HasDicomFile = true;
            }
        }

        private void OnSaveDrawDataClick(Object sender, RoutedEventArgs e)
        {
            this.dicomViewer.SaveDrawDataToFile();
        }

        private void OnSaveAllDrawDataClick(Object sender, RoutedEventArgs e)
        {
            this.dicomViewer.SaveAllDrawDataToFile();
        }

        private void OpenDrawDataClick(Object sender, RoutedEventArgs e)
        {
            var folder = Path.Combine(Environment.CurrentDirectory, "Draws");

            if (!Directory.Exists(folder))
                Directory.CreateDirectory("Draws");

            var dlg = new OpenFileDialog
            {
                Filter = "XML files (*.xml)|*.xml",
                DefaultExt = "xml",
                InitialDirectory = folder,
                RestoreDirectory = true
            };

            if (dlg.ShowDialog() == true)
            {
                this.dicomViewer.LoadDrawDataFromFile(dlg.FileName);
            }
        }

        private void OnPrintClick(Object sender, RoutedEventArgs e)
        {
            this.dicomViewer.Print();
        }

        private void OnSaveImageClick(Object sender, RoutedEventArgs e)
        {
            this.dicomViewer.SaveBitmapToFile();
        }

        private void OnLoadAllDrawDataClick(object sender, RoutedEventArgs e)
        {
            this.dicomViewer.LoadAllDrawDataFromFile(null);
        }
    }
}
